﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Webdisk;
using System.Net.Sockets;
using static Webdisk_New.UserHome;

namespace Webdisk_New
{
    public partial class FileInformation : Form
    {
        public FileInformation()
        {
            InitializeComponent();
        }
        private Point mPoint;

        private void FileInformation_Load(object sender, EventArgs e)
        {
           

            DBHelper.Conn.Close();
            SqlDataReader reader= DBHelper.GetDataReader(string.Format("select * from Information where FileName='{0}'",Temp.FileName));
            if (reader.Read())
            {
                FileName.Text = reader["FileName"].ToString();
                string FileExt = reader["FileExt"].ToString();
                FileExtlab.Text = FileExt;
                Size.Text = (double.Parse(reader["FileSize"].ToString())/1024.0/1024.0).ToString("0.00")+"MB";
                Time.Text = reader["Stime"].ToString();

                if (FileExt.ToLower() == "pptx" || FileExt.ToLower() == "ppt")
                {
                    //Icon.Image = ImageIndex[12];
                    Icon.Image = imageList1.Images[12];
                }
                else if (FileExt.ToLower() == "txt")
                {
                    Icon.Image = imageList1.Images[9];
                }
                else if (FileExt.ToLower() == "docx" || FileExt.ToLower() == "doc")
                {
                    Icon.Image = imageList1.Images[4];
                }
                else if (FileExt.ToLower() == "xlsx" || FileExt.ToLower() == "xls")
                {
                    Icon.Image = imageList1.Images[1];
                }
                else if (FileExt.ToLower() == "exe")
                {
                    Icon.Image = imageList1.Images[5];
                }
                else if (FileExt.ToLower() == "mp4" || FileExt.ToLower() == "wmv" || FileExt.ToLower() == "rmvb" || FileExt.ToLower() == "3gp" || FileExt.ToLower() == "avi" || FileExt.ToLower() == "mkv" || FileExt.ToLower() == "flv")
                {
                    Icon.Image = imageList1.Images[7];
                }
                else if (FileExt.ToLower() == "mp3" || FileExt.ToLower() == "wav" || FileExt.ToLower() == "flac" || FileExt.ToLower() == "ape")
                {
                    Icon.Image = imageList1.Images[13];
                }
                else if (FileExt.ToLower() == "zip" || FileExt.ToLower() == "rar" || FileExt.ToLower() == "7z" || FileExt.ToLower() == "iso")
                {
                    Icon.Image = imageList1.Images[15];
                }
                else if (FileExt.ToLower() == "jpg" || FileExt.ToLower() == "bmp" || FileExt.ToLower() == "png" || FileExt.ToLower() == "gif" || FileExt.ToLower() == "jpeg" || FileExt.ToLower() == "ico")
                {
                    Icon.Image = imageList1.Images[8];
                }
                else if (FileExt.ToLower() == "torrent")//种子文件
                {
                    Icon.Image = imageList1.Images[14];
                }
                else if (FileExt.ToLower() == "html" || FileExt.ToLower() == "htm")//网页文件
                {
                    Icon.Image = imageList1.Images[2];
                }
                else if (FileExt.ToLower() == "dll")                            //dll文件
                {
                    Icon.Image = imageList1.Images[0];
                }
                else
                {
                    Icon.Image = imageList1.Images[11];
                }


            }

            reader.Close();
            DBHelper.Conn.Close();


        }

        private void FileName_Click(object sender, EventArgs e)
        {
            FileName.ReadOnly = false;
        }

        private void FileInformation_Click(object sender, EventArgs e)
        {
            FileName.ReadOnly = true;

        }

        private void FileName_KeyPress(object sender, KeyPressEventArgs e)
        {
            bool illegal = false;
            if (e.KeyChar==13)
            {
                for (int i=0;i<FileName.Text.Length;i++)
                {
                    if (FileName.Text.Substring(i,1)=="\\"|| FileName.Text.Substring(i, 1) == "/" || FileName.Text.Substring(i, 1) == ":" || FileName.Text.Substring(i, 1) == "*" || FileName.Text.Substring(i, 1) == "?" || FileName.Text.Substring(i, 1) == "<" || FileName.Text.Substring(i, 1) == ">" || FileName.Text.Substring(i, 1) == "|" || FileName.Text.Substring(i, 1) == "\"")
                    {
                        illegal = true;
                        break;
                    }

                }
                if (illegal == true)
                {
                    MessageBox.Show("文件名中不能包含下列任何字符\r\n\\ / : * ? \" < > |");
                }
                else {
                    FileName.ReadOnly = true;
                    
                }
            }
        }
        //static TcpClient TcpClient;//创建监听


        private void Save_Click(object sender, EventArgs e)
        {
            

            



            DBHelper.ExecuteNonQuery(string.Format("update Information set FileName='{0}',Stime=GETDATE()  where loginName='{1}' and FileName='{2}'", FileName.Text,Temp.TmeploginName,Temp.FileName));
            Temp.Update = true;
            this.Close();
            
        }
        //实现窗口拖动
        private void FileInformation_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void FileInformation_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }

        private void label5_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void label5_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }
        //关闭离开
        private void pic_Close_MouseLeave(object sender, EventArgs e)
        {
            pic_Close.Image = null;
        }
        //关闭点击

        //关闭经过
        private void pic_Close_MouseEnter(object sender, EventArgs e)
        {
            pic_Close.Image = imageList1.Images[16];
        }

        private void pic_Close_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
